<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>绝对定位水平居中</title>

    <style>
        * {
            margin: 0;
            padding: 0;
        }
        .box {
            position: absolute;
            left: 50%;
            margin-left: -100px;
            background-color: pink;
            width: 200px;
            height: 200px;
        }
        .box1 {
            position: absolute;
            top: 50%;
            margin-top: -100px;
            background-color: red;
            width: 200px;
            height: 200px;
        }
        .box2 {
            position: absolute;
            top: 50%;
            left: 50%;
            margin-left: -100px;
            margin-top: -100px;
            width: 200px;
            height: 200px;
            background-color: skyblue;
        }
    </style>

</head>
<body>
    
    <div class="box">左右居中</div>
    <div class="box1">上下居中</div>
    <div class="box2">屏幕中间</div>

</body>
</html>